// 输入内容离开编辑框后事件监听   onpropertychange  click   MouseEvent  mouseleave
function scal(e) {
  try {
    // 序列化获取form表单数据
    // var tmp = $("#addForm").serializeArray(); //表单序列化 = > 直接将表单的数据内容提取为查询字符串格式(键=值)
    // var fromData =  (tmp); //格式化数据
    // console.log("formData:", fromData, typeof fromData)
    // 取全部柜型种类数据及计算种类数量boxType
    var boxType = $(".boxType"); //柜型集合获取
    var boxCount = $(".boxCount"); // 柜型及数量获取
    // 材料成本获取
    var PackMates = $(".PackMates"); //包材集合
    var devices = $(".devices"); //电控集合
    var Pcb = $(".Pcb"); //主板集合
    var fenmo = $(".fenmo"); //粉末集合
    var others = $(".others"); //其他
    var lock = $(".lock"); //电控锁集合
    var wujin = $(".wujin"); //五金
    var cable = $(".cable"); //线材
    var cableCost = $(".cableCost"); //材料合计成本
    var Price = $(".Price"); //报价单价
    var AFMWTC = $(".AFMWTC"); //不含税材料+人工成本
    var AFCMTC = $(".AFCMTC"); //含税材料+人工，获取当列柜型单台含税成本字符串
    var ArtificialCost = $(".ArtificialCost"); //人工+制造费
    var metals = $(".metals"); //钢材集合
    var GrossProfit = $(".GrossProfit"); //毛利额-单价
    var GPfM = $(".GPfM"); //获取各列计算毛利率集合
    var SalesComm = $(".SalesComm"); //销售提成
    var transportation = $(".transportation"); //
    var NetAmount = $(".NetAmount"); //获取
    var Handling = $(".Handling"); //获取搬运费集合
    var transportation = $(".transportation"); //运输物流
    var afterSales = $(".afterSales"); //获取售后维保费集合
    var proFitMargin = $(".proFitMargin"); //单列净利率集合
    var PriceTotal2 = $(".PriceTotal2");
    var TotalNet = $(".TotalNet"); //净利率润合计
    var costCombined = $(".costCombined");

    // 不含税材料成本总计数据及计算小计 cableCost   cableCostTotal
    var PackMatesTotal = 0;
    var deviceTotal = 0;
    var cableTotal = 0;
    var cableCostTotal = 0; //总材料成本容器
    var boxCountTotal = 0; //总柜子数量容器
    var AlMWTCostTotal = 0;
    var AFCMTC_Total = 0; //不含税总成本
    var ACMaT_Total = 0; //含税总成本
    var PriceTotal_2 = 0; //总报价
    var metalTotal = 0; //
    var TypeCount = 0; //种类合计容器
    var GrossProfitTotal = 0; //毛利总额容器
    var afterSalesTotal = 0; //售后维保费总容器
    var transportationTotal = 0; //物流运输费总计算容器
    var HandlingTotal = 0; //装卸总搬运费容器
    var SalesCommissionsTotal = 0; //销售提成总额
    var NetAmountTotal = 0; //利润总额容器
    var proFitMarginTotal = 0; //平均利率容器
    var PriceTotalTotal = 0; //总报价
    var TotalNetTotal = 0; //净利润合计
    var costCombinedTotal = 0; //总含税成本
    for (j = 0, len = AFMWTC.length; j < len; j++) {
      // 柜型种类合计  有计算金额时候才需要加parsefloat 及tofexid(2)
      if ($(boxType[j]).val() != "" && $(boxType[j]).val() != null) {
        TypeCount++;
      } else {
      }
      $("input[name='TypeCount']").val(TypeCount); //写出柜子种类数值

      // 取柜子采购数量数据及小计计算boxCount;
      var Nmb_boxCount = $(boxCount[j]).val();
      if (Nmb_boxCount == "" || Nmb_boxCount == null) {
        Nmb_boxCount = 0;
        $(boxCount[j]).val(0);
      }

      boxCountTotal += parseInt(Nmb_boxCount);
      $("input[name='boxCountTotal']").val(boxCountTotal); //写出总柜子数量合计

      // 各列柜型材料不含税成本;
      var str_PackMates = $(PackMates[j]).val();
      if (str_PackMates == "" || str_PackMates == null) {
        str_PackMates = 0;
        $(PackMates[j]).val(0);
      }
      var number_PackMates = parseFloat(str_PackMates); //.toFixed(2);

      var str_devices = $(devices[j]).val();
      if (str_devices == "" || str_devices == null) {
        str_devices = 0;
        $(devices[j]).val(0);
      }
      var number_devices = parseFloat(str_devices); //.toFixed(2);

      var str_Pcb = $(Pcb[j]).val();
      if (str_Pcb == "" || str_Pcb == null) {
        str_Pcb = 0;
        $(Pcb[j]).val(0);
      }
      var number_Pcb = parseFloat(str_Pcb); //.toFixed(2);

      var str_fenmo = $(fenmo[j]).val();
      if (str_fenmo == "" || str_fenmo == null) {
        str_fenmo = 0;
        $(fenmo[j]).val(0);
      }
      var number_fenmo = parseFloat(str_fenmo); //.toFixed(2);

      var str_metals = $(metals[j]).val();
      if (str_metals == "" || str_metals == null) {
        str_metals = 0;
        $(metals[j]).val(0);
      }
      var number_metals = parseFloat(str_metals); //.toFixed(2);

      var str_others = $(others[j]).val();
      if (str_others == "" || str_others == null) {
        str_others = 0;
        $(others[j]).val(0);
      }
      var number_others = parseFloat(str_others); //.toFixed(2);

      var str_lock = $(lock[j]).val();
      if (str_lock == "" || str_lock == null) {
        str_lock = 0;
        $(lock[j]).val(0);
      }
      var number_lock = parseFloat(str_lock); //.toFixed(2);

      var str_wujin = $(wujin[j]).val();
      str_wujin = isStr(str_wujin);
      //   if (str_wujin == "" || str_wujin == null) {
      //     str_wujin = 0;
      $(wujin[j]).val(str_wujin);
      //     // alert("不可为空");
      //   }
      var number_wujin = parseFloat(str_wujin); //.toFixed(2)

      var str_cable = $(cable[j]).val();
      if (str_cable == "" || str_cable == null) {
        str_cable = 0;
        $(cable[j]).val(0);
      }
      var number_cable = parseFloat(str_cable); //.toFixed(2)

      //#***************#//   合计材料
      cableCostTotal = parseFloat(number_PackMates + number_devices + number_Pcb + number_fenmo + number_metals + number_others + number_lock + number_wujin + number_cable).toFixed(2);
      $(cableCost[j]).val(cableCostTotal); //写出单柜型不含税材料成本合计

      //获取当列柜型单台含税成本字符串
      //#***************#// var Number_rote = parseFloat(str_rote).toFixed(2);
      var str_box = $(boxCount[j]).val(); //获取该列柜子数量
      var number_box = parseInt(str_box); //柜子数据转整数
      var str_AFCMTC = $(AFCMTC[j]).val();

      //#***************#//计算单柜型列不含税材料成本;
      if ($(cableCost[j]).val() == "" || $(cableCost[j]).val() == 0) {
        $(cableCost[j]).val(0);
      }
      var str_cableCost = $(cableCost[j]).val(); //获取该柜型材料成本
      if (isnUll(str_cableCost)) {
        str_cableCost = 0;
        $(cableCost[j]).val();
      }
      var number_cableCost = parseFloat(str_cableCost); //材料成本转整数

      //#***************#// 不含税成本处理
      var str_ArtificialCost = $(ArtificialCost[j]).val(); //获取人工成本
      if (str_ArtificialCost == "" || str_ArtificialCost == 0) {
        str_ArtificialCost = 0;
        $(ArtificialCost[j]).val(0);
      }
      var number_ArtificialCost = parseFloat(str_ArtificialCost);
      AlMWTCostTotal = parseFloat(number_cableCost + number_ArtificialCost).toFixed(2);
      $(AFMWTC[j]).val(AlMWTCostTotal); //写出各列不含税成本

      //#***************#// 含税人工&制造费&材料总成本单柜合计，小计计算 AFCMTC   ACMaT_Total;
      if ($(cableCost[j]).val() == "" || $(cableCost[j]).val() == null) {
        $(cableCost[j]).val(0);
      }
      var str_cableCost = $(cableCost[j]).val(); //获取该柜型材料成本
      if (isnUll(str_cableCost)) {
        str_cableCost = 0;
      }
      var number_cableCost = parseFloat(str_cableCost); //材料成本转整数
      if ($(ArtificialCost[j]).val() == "" || $(ArtificialCost[j]).val() == null) {
        $(ArtificialCost[j]).val(0);
      }
      var str_ArtificialCost = $(ArtificialCost[j]).val(); //获取人工成本
      if (isnUll(str_ArtificialCost)) {
        str_ArtificialCost = 0;
      }
      var number_ArtificialCost = parseFloat(str_ArtificialCost); //人工
      AFCMTC_Total = parseFloat(
        number_cableCost * 1.13 + number_ArtificialCost //含税材料+人工
      ).toFixed(2);
      $(AFCMTC[j]).val(AFCMTC_Total); //写出各列含税成本

      //#***************#// 计算该柜型含税成本合计 // 计算全部柜型含税材料+人工成本X数量合计;
      if (str_AFCMTC == null || str_AFCMTC == "" || str_AFCMTC == NaN) {
        str_AFCMTC = 0;
      }
      var number_AFCMTC = parseFloat(str_AFCMTC).toFixed(4); //获取不含税成本单列数据
      if (str_AFCMTC == "" || str_AFCMTC == null) {
        str_AFCMTC = 0;
      }
      if (number_box == "" || number_box == null) {
        number_box = 0;
      }
      var hscb = parseFloat(number_AFCMTC * number_box).toFixed(2); //含税成本*数量
      ACMaT_Total = FloatAdd(hscb, ACMaT_Total); //自相加
      $("input[name='ACMaT_Total']").val(ACMaT_Total); //单列循完后写出该行合计含税总成本

      // 含税+毛利对外报价计算 Price   PriceTotal;
      //#***************#//计算各列成本X毛利的含税单价报价;
      var str_rote = $("#rote").val(); //获取毛利率 直接获取数据
      var number_rote = toPoint(str_rote); //获取了毛利率数
      var price_bj = number_AFCMTC / (1 - number_rote); //毛利报价=浮点数含税材料+人工总成本/毛利率
      var price_bj = (number_cableCost * 1.13) / (1 - number_rote) + number_ArtificialCost; //毛利报价=浮点数含税材料成本/毛利率+人工
      var PriceTotal = price_bj.toFixed(2); //当前计算出的报价转成浮点数
      $(Price[j]).val(PriceTotal); //写出各列柜型的单台含税毛利报价

      //#***************# // 以下是合计单列报价X数量的+全部柜型的合计;
      var one_box_bj = parseFloat(PriceTotal * number_box).toFixed(2); //计算出单柜型单价X数量
      PriceTotal_2 = FloatAdd(one_box_bj, PriceTotal_2); //计算出每个柜型X数量的全部报价合计
      $("input[name='PriceTotal']").val(PriceTotal_2); //写出每个柜型报价X数量总报价

      //#***************#// 毛利额=毛利外报价-含税成本，总毛利小计计算 GrossProfit   GrossProfitTotal;
      var str_priceX = $(Price[j]).val(); //获取当前列毛利报价值
      if (str_priceX == "" || str_priceX == null || str_priceX == NaN) {
        str_priceX = 0;
      }
      var str_AFCMTCX = $(AFCMTC[j]).val(); //获取当前列含税成本
      if (str_AFCMTCX == "" || str_AFCMTCX == null || str_AFCMTCX == NaN) {
        str_AFCMTCX = 0;
      }
      var GrossProfit = FloatSub(PriceTotal, AFCMTC_Total); //含税毛利报价-减含税成本
      $(".GrossProfit").eq(j).val(GrossProfit); //写出毛利额

      //#***************#// 总毛利额计算;
      oneBox_Gpf = GrossProfit * number_box; //获取单柜型的全部毛利
      GrossProfitTotal = FloatAdd(oneBox_Gpf, GrossProfitTotal);
      $("input[name='GrossProfitTotal']").val(GrossProfitTotal); //写出毛利报价总额

      //#***************#// 单列核算毛利率计算;
      var float_gpfm = FloatSub(PriceTotal, number_AFCMTC);
      var rote_gpfm = parseFloat((float_gpfm * 100) / (PriceTotal * 100)).toFixed(6);
      var Percent_gpfm = (number_rote * 100).toFixed(2);
      // console.log((float_gpfm * 100) / (PriceTotal * 100), rote_gpfm, Percent_gpfm)
      $(GPfM[j]).val(Percent_gpfm);
      $("input[name='GrossProfitMarginTotal']").val(Percent_gpfm); //写出报价毛利率

      //#***************#// 售后维保费计算afterSales  afterSalesTotal
      var Server_rote = $("#Server_rote").val(); //获取服务费率
      if (Server_rote == "" || Server_rote == null) {
        Server_rote = 0;
      }
      var flaot_server_rote = parseFloat(Server_rote).toFixed(2);
      var str_afterSalesRote = parseFloat(PriceTotal * flaot_server_rote).toFixed(2);
      var number_afterSalePrice = parseFloat(str_afterSalesRote / 100).toFixed(2);
      $(afterSales[j]).val(number_afterSalePrice);

      //#***************#//写出总售后维保费合计
      var number_total_afterSalePrice = number_afterSalePrice * number_box; //单列售后服务费X单列数量
      afterSalesTotal = FloatAdd(number_total_afterSalePrice, afterSalesTotal); //没列累加
      // console.log(number_afterSalePrice, number_total_afterSalePrice, afterSalesTotal, Nmb_boxCount, number_box);
      $("input[name='afterSalesTotal']").val(afterSalesTotal); //写出报价毛利率

      //#***************#//物流运输费计算transportation transportationTotal
      var str_transportation = $(transportation[j]).val(); //.toFixed(2);
      if (isnUll(str_transportation)) {
        str_transportation = 0;
        $(transportation[j]).val(0);
      }
      var str_transportation2 = parseFloat(str_transportation * number_box).toFixed(2);
      transportationTotal = FloatAdd(str_transportation2, transportationTotal);
      $("input[name='transportationTotal']").val(transportationTotal); //写出物流费合计

      //#***************#//装卸搬运费计算  Handling HandlingTotal
      var str_Handling = $(Handling[j]).val(); //.toFixed(2);
      if (isnUll(str_Handling)) {
        str_Handling = 0;
        $(Handling[j]).val(0);
      }
      var str_Handling2 = parseFloat(str_Handling * number_box).toFixed(2);
      HandlingTotal = FloatAdd(str_Handling2, HandlingTotal);
      $("input[name='HandlingTotal']").val(HandlingTotal); //写出物流费合计

      //#***************#// 销售提成计算报价额5%;SalesComm SalesCommissionsTotal
      var str_salesComm = (PriceTotal * 0.05).toFixed(2); //parseFloat
      $(SalesComm[j]).val(str_salesComm); //写出销售提成
      //#***************#// 计算销售提成总额
      var str_salesComm_n = parseFloat(str_salesComm * number_box).toFixed(2);
      SalesCommissionsTotal = FloatAdd(str_salesComm_n, SalesCommissionsTotal);
      $("input[name='SalesCommissionsTotal']").val(SalesCommissionsTotal); //写出物流费合计

      //#***************# // 净利润额=毛利额-物流运输-装卸-售后维保-提成; NetAmount NetAmountTotal
      var str_afterSales = $(afterSales[j]).val();
      var fla_NetAmount1 = FloatSub(GrossProfit, str_Handling);
      var fla_NetAmount2 = FloatSub(fla_NetAmount1, str_transportation);
      var fla_NetAmount3 = FloatSub(fla_NetAmount2, str_afterSales);
      var fla_NetAmount4 = FloatSub(fla_NetAmount3, str_salesComm);
      $(".NetAmount").eq(j).val(fla_NetAmount4); //写出单列净利润，

      //#***************#// 计算净利润总额NetAmount NetAmountTotal
      var str_NetAmount_n = parseFloat(fla_NetAmount4 * number_box).toFixed(2); //单利润X数量
      NetAmountTotal = FloatAdd(str_NetAmount_n, NetAmountTotal);
      // console.log(str_NetAmount_n, NetAmountTotal);
      $("input[name='NetAmountTotal']").val(NetAmountTotal); //写出净利润总额合计

      //#***************#//净利率   proFitMargin proFitMarginTotal
      var flaot_proFitMargin = parseFloat((fla_NetAmount4 / PriceTotal) * 100).toFixed(2); //净利润除以报价
      $(proFitMargin[j]).val(flaot_proFitMargin); //写出单列净利率

      //#***************#// 计算平均净利率
      proFitMarginTotal = FloatAdd(flaot_proFitMargin, proFitMarginTotal); //累加单柜型净利率
      // console.log("j2:", j, fla_NetAmount4, PriceTotal, flaot_proFitMargin);
      if (j >= proFitMargin.length - 1) {
        var proFitMarginTotal_10 = parseFloat(proFitMarginTotal / 10).toFixed(2); //求平均数
        $("input[name='proFitMarginTotal']").val(proFitMarginTotal_10); //写出净利润总额合计
      }

      //#***************#// 含税毛利报价X数量合计  PriceTotal  PriceTotalTotal
      var onebox_price_add = parseFloat(PriceTotal * number_box).toFixed(2);
      $(PriceTotal2[j]).val(onebox_price_add);
      PriceTotalTotal = FloatAdd(onebox_price_add, PriceTotalTotal);
      // console.log("onebox_price_add:", onebox_price_add, PriceTotal, PriceTotalTotal, number_box);
      if (j >= proFitMargin.length - 1) {
        $("input[name='PriceTotalTotal']").val(PriceTotalTotal); //写出总含税报价合计
      }

      //#***************#// 净利润合计 TotalNet TotalNetTotal
      var str_TotalNet = parseFloat(fla_NetAmount4 * number_box).toFixed(2); //净利润X数量
      $(TotalNet[j]).val(str_TotalNet); //写出单列净利润
      TotalNetTotal = FloatAdd(str_TotalNet, TotalNetTotal);
      if (j >= proFitMargin.length - 1) {
        $("input[name='TotalNetTotal']").val(TotalNetTotal); //写出总净利润合计
      }

      //#***************#//含税成本总计计算 costCombined  costCombinedTotal
      var one_costCombined_ride = parseFloat(AFCMTC_Total * number_box).toFixed(2);
      $(costCombined[j]).val(one_costCombined_ride);
      costCombinedTotal = FloatAdd(one_costCombined_ride, costCombinedTotal); //用上方计算单柜含税成本数据X柜型数量
      if (j >= proFitMargin.length - 1) {
        $("input[name='costCombinedTotal']").val(costCombinedTotal); //写出总净利润合计
      }
    }

    function isnUll(str) {
      if (str == "" || str == null || str == NaN || str == undefined) {
        return true;
      } else {
        return false;
      }
    }

    // 技术服务费计算
    //#***************#// 软件成本工费计算 soft_cost_Price  soft_unit soft_cost_Price soft_cost_time
    var soft_cost_time = $("input[name='soft_cost_time']").val(); //获取软件成本工时
    if (isnUll(soft_cost_time)) {
      soft_cost_time = 0;
      $("input[name='soft_cost_time']").val(0);
    }
    var soft_unit = $("input[name='soft_unit']").val(); //获取单价
    if (isnUll(soft_unit)) {
      soft_unit = 0;
    }
    var soft_cost_Price = parseFloat(soft_cost_time * soft_unit).toFixed(2);
    $("input[name='soft_cost_Price']").val(soft_cost_Price);

    //#***************#// 软件含税报价计算
    var kaifa_Time = $("input[name='kaifa_Time']").val(); //获取开发工时
    if (isnUll(kaifa_Time)) {
      kaifa_Time = 0;
      $("input[name='kaifa_Time']").val(0);
    }
    var soft_Total = parseFloat(kaifa_Time * soft_unit).toFixed(2);
    $("input[name='soft_Total']").val(soft_Total);

    //#***************#// 软件报价毛利率计算  soft_GPM
    var soft_Cost_subb = FloatSub(soft_Total, soft_cost_Price);
    var soft_GPM = parseFloat((soft_Cost_subb / soft_Total) * 100).toFixed(2);
    $("input[name='soft_GPM']").val(soft_GPM);

    //#***************#// 柜子成本工费计算  box_unit box_desiner_time box_cost_time box_cost_Price box_BGM box_Total
    var box_cost_time = $("input[name='box_cost_time']").val(); //获取柜子成本工时
    if (isnUll(box_cost_time)) {
      box_cost_time = 0;
      $("input[name='box_cost_time']").val(0);
    }
    var box_unit = $("input[name='box_unit']").val(); //获取单价
    if (isnUll(box_unit)) {
      box_unit = 0;
      $("input[name='box_unit']").val(0);
    }
    var box_cost_Price = parseFloat(box_cost_time * box_unit).toFixed(2); //计算费用
    $("input[name='box_cost_Price']").val(box_cost_Price); //写出软件定制费报价

    //#***************#// 柜子定制设计含税报价计算
    var box_desiner_time = $("input[name='box_desiner_time']").val(); //获取开发工时
    if (isnUll(box_desiner_time)) {
      box_desiner_time = 0;
      $("input[name='box_desiner_time']").val(0);
    }
    var box_Total = parseFloat(box_desiner_time * box_unit).toFixed(2);
    $("input[name='box_Total']").val(box_Total); //写出柜型定制费报价

    //#***************#// 柜子定制费报价毛利率计算
    var box_Cost_subb = FloatSub(box_Total, box_cost_Price);
    var box_BGM = parseFloat((box_Cost_subb / box_Total) * 100).toFixed(2);
    $("input[name='box_BGM']").val(box_BGM); //写出柜子定制费报价毛利率

    //#***************#//#总报价计算 PriceTotal  ProjectTotalPrice soft_Total box_Total
    var priceAddsoftAddbox = FloatAdd(PriceTotalTotal, soft_Total);
    var ProjectTotalPrice = FloatAdd(priceAddsoftAddbox, box_Total);
    $("input[name='ProjectTotalPrice']").val(ProjectTotalPrice); //写出项目总含税报价
    // console.log("scal",ProjectTotalPrice);
  } catch (error) {
    errFlag = true;
    console.log(error);
    Toast("数据错误~", "error", 1000, 600, 50);
  } finally {
    // finallyCode - 无论 try / catch 结果如何都会执行的代码块\
    Toast("刷新计算~", "success", 1000, 500, 50);
  }
}
scal(); //首次运行一次计算刷新

// 浮点数相加方法
// alert(FloatAdd("0", "4.4199"))
function FloatAdd(arg1, arg2) {
  var r1, r2, m;
  try {
    r1 = arg1.toString().split(".")[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = arg2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }
  m = Math.pow(10, Math.max(r1, r2));
  var number_m = (arg1 * m + arg2 * m) / m;
  return parseFloat(number_m).toFixed(2);
  // return number_m;
}

// 浮点数相减方法
// alert(FloatSub("9999.22", "5555.11"))
function FloatSub(arg3, arg4) {
  var r11, r22, m2;
  try {
    r11 = arg3.toString().split(".")[1].length;
  } catch (e) {
    r11 = 0;
  }
  try {
    r22 = arg4.toString().split(".")[1].length;
  } catch (e) {
    r22 = 0;
  }
  m2 = Math.pow(10, Math.max(r11, r22));
  var number_m2 = (arg3 * m2 - arg4 * m2) / m2;
  return parseFloat(number_m2).toFixed(2);
  // return number_m;
}

// 百分数转小数
function toPoint(str_percent) {
  var str_percent2 = str_percent.replace(".", "");
  str_percent2 = "0." + str_percent2;
  var Nub = parseFloat(str_percent2).toFixed(4);
  // console.log("毛利率:(%)", Nub);
  return Nub;
}

// 点击计算触发提示方法，弥补onchange内容改变事件不能在触发后获取当前输入的数据的缺陷，
$("#scal").click(function (e) {
  scal(e);
});

// 另外一种监听表单改变事件方法，
$("input").on("oninput", "input", function (e) {
  // alert("$oninput", $(this))
});

// input输入事件
$("input[type='number']").change(function (e) {
  var number_thisVal = parseFloat($(this).val());
  //   alert(number_thisVal);
  if (number_thisVal < 0) {
    $(this).val(0);
  } else {
    $(this).val(number_thisVal);
  }
});

$("#addForm").on("blur", "input", function (e) {
  scal(e);
});

$("input[type='text']").change(function (e) {
  //消除0开头
  var number_thisVal = parseFloat($(this).val());
  //   $(this).val(number_thisVal);
});

// $("#addForm").bind("input mouseleave", function(e) {
//     scal(e);
// });

$("#addForm").bind("input oninput", function (e) {
  scal(e);
});

// $("#addForm").bind("input click", function(e) {});

// 当鼠标点击选择输入框时候清除输入框内容,同时设置小计计算内容，onchange事件无法替代
$("#addForm").on("click", ".form-control", function () {
  $(this).val("");
});

// $("input").on("mouseleave", ".form-control", function() {});

$("#addForm").on("input oninput", function (e) {
  // alert("$input", e)
});

// $("#addForm input").mouseleave(function(e) {
//     scal(e);
// });

// 限制品名和品号输入中文
$(".boxSN").bind("input onpropertychange ", function (e) {
  var str = $(this).val();
  var str2 = str.replace(/[\u0391-\uFFE5]/gi, "");
  $(this).val(str2);
  // console.log(str2);
});
$(".size").bind("input onpropertychange ", function (e) {
  var str = $(this).val();
  var str2 = str.replace(/[\u0391-\uFFE5]/gi, "");
  $(this).val(str2);
  // console.log(str2);
});

//nput失去焦点后 如果值发生改变后，触发
$("#addForm > input[type='number']").blur(function (e) {
  var str = $(this).val();
  if (str == "" || isNaN(str) || str == null || str == undefined) {
    str = 0;
  }
  $(this).val(str);
});
$("#addForm").on("blur", "input[type='number']", function (e) {
  var str = $(this).val();
  if (str == "" || isNaN(str) || str == null || str == undefined) {
    str = 0;
  }
  $(this).val(str);
});

// 清除非法字符，同时计算字段数量
var jsonLength, jsonLength2; //有效字段数和总字段数
var submitDataJsonArr = [];
var jsonLength, jsonLength2; //有效字段数和总字段数
var submitDataJsonArr = [];
var rote = $("#rote").val(); //获取毛利率 直接获取数据
var Server_rote = $("#Server_rote").val(); //获取服务费率
var sid = $("#sid").html(); //获取sid
var sidStr = sid.toString();
var cname = getCookie("cname");
var userType = getCookie(userType);
var customerType = $("input[name='customerType']").val() || "利润客户";

function remove_empty(obj) {
  rote = $("#rote").val(); //获取毛利率 直接获取数据
  Server_rote = $("#Server_rote").val(); //获取服务费率
  sid = $("#sid").html(); //获取sid
  userType = getCookie("userType");
  cname = getCookie("cname");
  sidStr = sid.toString();

  obj.rote = API.rote;
  obj.Server_rote = API.Server_rote;
  obj.sid = API.sid;
  // obj.other = API.other;
  obj.status = API.status;
  obj.userType = API.userType;
  obj.engineer = API.engineer;
  obj.customerType = API.customerType;

  jsonLength2 = 0;
  jsonLength = 0;
  submitDataJsonArr = [];
  //   obj.ak = API.ak; //添加ak

  // console.log(obj.sid, obj.rote, obj.other);
  var obj2 = {}; //临时json容器 装全部字段
  for (let k in obj) {
    v = obj[k];
    if (v === "" || v === null || v === NaN || v === undefined) {
      delete obj[k];
      // console.log(v)
      // console.log(k)
    } else {
      // console.log(v);//键值
      // console.log(k);//键名
      obj2[k] = v; //bij对象数据的键名在用变量代替时候需要用下标而不是点
      jsonLength++;
      submitDataJsonArr.push(obj2);
    }
    jsonLength2++; //总字段数
    obj2[k] = v;
  }
  // obj.totalDataCount = jsonLength;//剔除为空的字段
  // obj2.totalDataCount = jsonLength2;//JSON全字段返回
  return obj;
}
//

//封装一个处理表单序列化数据格式
function formatData(array) {
  var data = {};
  //处理需要提交的数据
  $.each(array, function (index, item) {
    data[item.name] = item.value;
  });
  return data;
}

// 判断内容是否为空
function isStr(str) {
  if (str == "" || str == null || str == NaN || str == undefined) {
    return 0;
  } else {
    return str;
  }
}

// guanbi关闭模态框
$(".updateClose").click(function (e) {
  var iwindow = window.parent.$("#eidithModal"); //获取父级关闭他
  $(iwindow).click(); //updateClose
  // alert(e)
});

//===========================================键盘监听事件======================================
$(document).keydown(function (event) {
  //   event.preventDefault();
  if (event.keyCode == 13) {
  } else if (event.keyCode == 27) {
    //ESC键盘
    $(".modal").hide(500);
    $(".modal-backdrop").hide(200);
    $(".close").trigger("click");
    $("#loading").hide();
  }
});
